package com.fastwire.carriere.rad.dao;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.fastwire.carriere.common.constant.AppConstants;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.hibernate.daoimpl.AbstractDAOImpl;
import com.fastwire.carriere.hibernate.daoimpl.HibernateUtil;
import com.fastwire.carriere.rad.entity.Rad;

/**
 * @author Jun on Apr 30, 2012, 2012
 */
public abstract class AbstractRadDaoImp extends AbstractDAOImpl
{
    public void saveOrUpdateAllRad(List<Rad> objList) throws DAOException
    {
        Session session = HibernateUtil.openSession();
        try
        {
            int count = 0;
            session.beginTransaction();
            invalidateRad(session);

            for (Object obj : objList)
            {
                // session.saveOrUpdate(obj);
                session.persist(obj);
                if (++count % 50 == 0)
                {
                    session.flush();
                    session.clear();
                }
                CommonLogger.info(this, "total entities in session"
                        + session.getStatistics().getEntityCount());

            }

            session.getTransaction().commit();

        }
        catch (Exception ex)
        {
            if (session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            throw new DAOException(ex);
        }
        finally
        {
            HibernateUtil.close(session);
        }
    }
    
    public void saveOrUpdateAll(List objList) throws DAOException
    {
        if (null == objList || objList.size() <= 0)
        {
            return;
        }
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();

        try
        {
            int count = 0;
            for (Object obj : objList)
            {
                // session.saveOrUpdate(obj);
                session.save(obj);
                if (++count % 50 == 0)
                {
                    session.flush();
                    session.clear();
                }
                CommonLogger.info(this, "total entities in session"
                        + session.getStatistics().getEntityCount());
            }

            tx.commit();
        }
        catch (Exception ex)
        {
            if (tx.isActive())
            {
                tx.rollback();
            }
            throw new DAOException(ex);
        }
        finally
        {
            HibernateUtil.close(session);
        }
    }
    
    public List<String> getAllValuesOfOneFiled(String sql)
    {
        Session session = HibernateUtil.openSession();
        List<String> list = null;
        try
        {
            session.beginTransaction();
            //session.createSQLQuery("SELECT unique(ipAddr) FROM ce_rad WHERE status = active");
            
            SQLQuery query= session.createSQLQuery(sql);
    
            list = query.list();
            
            session.getTransaction().commit();
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            session.close();
        }
        
        return list;
    }

    
    private void invalidateRad(Session session) throws DAOException
    {
        org.hibernate.Query query = session.createQuery("from Rad as r where r.status != :status and r.invalidateDate is null");
        
        query.setParameter("status", AppConstants.NE_STATUS_INACTIVE);
        ScrollableResults scrollableResults = query.setFetchSize(1000).scroll(
                ScrollMode.FORWARD_ONLY);
        
        for (int count=1; scrollableResults.next(); ++count)
        {
            System.out.println("count is:" + count);
            Object object = scrollableResults.get(0);

            if (object instanceof Rad)
            {
                ((Rad) object).invalidate();
            }

            if (count % 50 == 0)
            {
                session.flush();
                session.clear();
            }
        }

        session.flush();
        session.clear();
    }
    
}
